rm(list=ls())
library(data.table)
library(xtable)
library(tidyr)
library(dplyr)
library(tibble)
library(readr)
library(stringr)


files = paste0('results/',
              c('future-results-2012-2016.Rdata',
               
                'future-results-2012-2016-pretrend.Rdata',
                
                'future-results-2012-2016-full-knn.Rdata',
               
                'future-results-2012-2016-dr.Rdata',
                
                'future-results-2012-2016-d1600.Rdata',
                'future-results-2012-2016-k100.Rdata',
                'future-results-2012-2016-k500.Rdata',
                'future-results-2012-2016-block.Rdata',
                'future-results-2012-2016-block-group.Rdata',
                
                
                
                'future-results-2012-2016-aspatial.Rdata',
                'future-results-2012-2016-no-bg-controls.Rdata',
                
                
                'future-results-2012-2016-new-neighbors.Rdata'
               
                )
               )

results = rbindlist(lapply(files, FUN=function(file){
  
  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
    # print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    out[,N:=m$N]
    out[,`R-Squared`:=m$r.squared]
    out[,`Adjusted R-Squared`:=m$adj.r.squared]
    out[,Sigma:=m$sigma]
    out[,FStat:=m$fstat]
    if('Cluster s.e.'%in%names(out)){
      out[,SE:=`Cluster s.e.`]
      out[,SE.type:='Cluster']
      out[,`Cluster s.e.`:=NULL]
    } else {
      out[,SE:=`Robust s.e`]
      out[,SE.type:='Robust']
      out[,`Robust s.e`:=NULL]
    }
    
    
    return(out)
    
  }))
  
  l[grepl('2008',file),Year1:='2008']
  l[grepl('2008',file),Year2:='2012']
  l[grepl('2020',file),Year1:='2016']
  l[grepl('2020',file),Year2:='2020'] 
  l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
  l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
  l[,file:=file]
  
  return(l)
}))
 

### 2012-2016

t = data.table(Treatment = c('Main', 'Main',
                             'Pretrend', 'Pretrend',
                             'Aspatial', 'Aspatial',
                             'Dem. Ratio', 'Dem. Ratio',
                             'Include Same Household', 'Include Same Households',
                             '100 Neighbors', '100 Neighbors',
                             '500 Neighbors', '500 Neighbors',
                             'Mile Radius', 'Mile Radius',
                             'New Neighbors', 'New Neighbors',
                             'Census Block', 'Census Block',
                             'Census Block Group', 'Census Block Group',
                             'No Census controls', 'No Census controls'),
               
               
               Stat = rep(c('Coef','SE'),12),
               
               DED = c(
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE
                 
                 
               ),
               
               DER = c(
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE
                 
                 
               ),
               
               DEI = c(
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE
                 
                 
               ),
               
               RED = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE
                 
                 
               ),
               
               RER = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE
                 
                 
               ),
               
               REI = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/future-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/future-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/future-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/future-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/future-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/future-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/future-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/future-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/future-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE
                 
                 
               )
               
               
               
               
               
               
               
               
               
               )



t[,DED:=ifelse(Stat=='Coef',round(DED,3),paste0('(',round(DED,3),')'))]
t[,DER:=ifelse(Stat=='Coef',round(DER,3),paste0('(',round(DER,3),')'))]
t[,DEI:=ifelse(Stat=='Coef',round(DEI,3),paste0('(',round(DEI,3),')'))]
t[,RED:=ifelse(Stat=='Coef',round(RED,3),paste0('(',round(RED,3),')'))]
t[,RER:=ifelse(Stat=='Coef',round(RER,3),paste0('(',round(RER,3),')'))]
t[,REI:=ifelse(Stat=='Coef',round(REI,3),paste0('(',round(REI,3),')'))]
t[Stat=='SE',Treatment:='']
t[,Stat:=NULL]





out = gt(t) %>%
  cols_label(
    `DED` = 'Democrats',
    `DER` = 'Republicans',
    `DEI` = 'Non-Partisans',
    `RED` = 'Democrats',
    `RER` = 'Republicans',
    `REI` = 'Non-Partisans'
  )%>%
  tab_spanner(label = 'DV: Democratic Registration',columns = 2:4)%>%
  tab_spanner(label = 'DV: Republican Registration',columns = 5:7)%>%
  tab_spanner(label = 'Downstream effects: 2012-2016 exposure change on 2016-2020 party switching',columns = 2:7)%>%
  
  as_latex()%>%
  as.character()%>%
  str_replace_all('longtable','tabular')


write_file(out, 'tables/TabS13.tex')  










